package org.dtrd.modules.archive.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.archive.entity.bean.report.DiabetesComorbidityReport;
import org.dtrd.modules.archive.entity.po.DtrdEntRdArchiveComorbidity;
import org.dtrd.modules.archive.mapper.DtrdEntRdComorbidityMapper;
import org.dtrd.modules.archive.service.IDtrdEntRdComorbidityService;
import org.dtrd.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.StringJoiner;

import static org.dtrd.config.system.Constant.DiabetesDiagnose.DIAGNOSE;

/**
 * @author 萧
 * @date 2021-10-01 22:47:26
 **/
@Service
@DS("multi-datasource1")
public class DtrdEntRdComorbidityServiceImpl extends ServiceImpl<DtrdEntRdComorbidityMapper, DtrdEntRdArchiveComorbidity> implements IDtrdEntRdComorbidityService {


    @Autowired
    private ISysDictService dictService;

    private final static String pubDossierHypertensionLevel = "pubDossierHypertensionLevel";

    private final static String pubDossierCoronaryType = "pubDossierCoronaryType";

    private final static String pubDossierAnginaType = "pubDossierAnginaType";

    private final static String pubDossierCardiacInsufficiency = "pubDossierCardiacInsufficiency";

    @Override
    public DiabetesComorbidityReport getPatientDiabetesComorbidityReport(Integer patientId) {
        DtrdEntRdArchiveComorbidity po = getPatientDiabetesComorbidity(patientId);
        DiabetesComorbidityReport report = null;
        if (po != null) {
            report = new DiabetesComorbidityReport().parseFromPo(po);
            // 高血脂
            Integer hypertension = po.getHypertension();
            if (hypertension != null && hypertension.equals(DIAGNOSE.getType())) {
                report.setHypertensionText(DIAGNOSE.getName());
                if (po.getHypertensionDate() != null) {
                    report.setHypertensionDate(DateUtil.formatDate(po.getHypertensionDate()));
                }
                StringJoiner joiner = new StringJoiner(";");
                Integer ifHypotensor = po.getIfHypotensor();
                if (ifHypotensor != null) {
                    joiner.add("是否服用降压药：" + Constant.YesOrNo.valueOf(ifHypotensor).getName());
                }
                Integer hypertensionLevel = po.getHypertensionLevel();
                if (hypertensionLevel != null) {
                    joiner.add("高血压分级：" + dictService.queryDictTextByKey(pubDossierHypertensionLevel, hypertensionLevel + ""));
                }
                if (StrUtil.isNotBlank(joiner.toString())) {
                    report.setHypertensionData(joiner.toString());
                }
            }
            // 心血管疾病
            Integer angiocardiopathy = po.getAngiocardiopathy();
            if (angiocardiopathy != null && angiocardiopathy.equals(DIAGNOSE.getType())) {
                report.setAngiocardiopathyText(DIAGNOSE.getName());
                if (po.getAngiocardiopathyDate() != null) {
                    report.setAngiocardiopathyDate(DateUtil.formatDate(po.getAngiocardiopathyDate()));
                }
                StringJoiner joiner = new StringJoiner("; ");
                // 冠心病类型
                Integer coronaryType = po.getCoronaryType();
                if (coronaryType != null) {
                    String text = dictService.queryDictTextByKey(pubDossierCoronaryType, coronaryType + "");
                    if (text.equals(Constant.other)) {
                        joiner.add("冠心病类型: " + po.getCoronaryOther());
                    } else {
                        joiner.add("冠心病类型: " + text);
                    }
                }
                Integer anginaType = po.getAnginaType();
                if (anginaType != null) {
                    joiner.add("心绞痛类型: " + dictService.queryDictTextByKey(pubDossierAnginaType, anginaType + ""));
                }
                Integer cardiovascularInfarction = po.getCardiovascularInfarction();
                if (cardiovascularInfarction != null) {
                    joiner.add("心机梗死: " + Constant.DiabetesDiagnose.valueOf(cardiovascularInfarction).getName());
                }
                Integer cardiacInsufficiency = po.getCardiacInsufficiency();
                if (cardiacInsufficiency != null) {
                    joiner.add("心功能不全类型: " + dictService.queryDictTextByKey(pubDossierCardiacInsufficiency, cardiacInsufficiency + ""));
                }
                String angiocardiopathyOther = po.getAngiocardiopathyOther();
                if (StrUtil.isNotBlank(angiocardiopathyOther)) {
                    joiner.add("其他心血管疾病: " + angiocardiopathyOther);
                }
                if (StrUtil.isNotBlank(joiner.toString())) {
                    report.setAngiocardiopathyExam(joiner.toString());
                }
            }

        }
        return report;
    }

    @Override
    public DtrdEntRdArchiveComorbidity getPatientDiabetesComorbidity(Integer patientId) {
        LambdaQueryWrapper<DtrdEntRdArchiveComorbidity> wrapper = Wrappers.lambdaQuery(DtrdEntRdArchiveComorbidity.class)
                .eq(DtrdEntRdArchiveComorbidity::getPatientId, patientId)
                .eq(DtrdEntRdArchiveComorbidity::getIsDel, 0)
                .last(" limit 1 ");
        return getOne(wrapper);
    }
}
